package fr.paulmasson.jfreex.db.dao;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;

import fr.paulmasson.jfreex.db.DbManager;
import fr.paulmasson.jfreex.db.bo.BoChannelInfo;
import fr.paulmasson.jfreex.utils.Common;
import fr.paulmasson.jfreex.utils.Wget;

public class DaoChannelInfo {

	public static void setup() {
		if (isEmpty()) {
			try {
				String data = Wget.wget(Common.channelInfoUrl);
				String[] data_lines = data.split("\n");
			
				for (String line : data_lines) {
					if (line.length() > 0) {
						String[] infos = line.split("	");
						BoChannelInfo info = new BoChannelInfo();
						info.setBunch(infos[2]);
						info.setChannel(Integer.parseInt(infos[1]));
						info.setName(infos[0]);
						DbManager.saveEntity(info);
					}
				}
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static int count() {
		return DbManager.get("from BoChannelInfo").size();
	}
	
	public static boolean isEmpty() {
		return count()==0;
	}
	
	public static void printTable(List<Object> data) {
		Dao.printTable(data);
	}

	public static String toFormattedString() {
		List<Object> list = DbManager.get("from BoChannelInfo");
		list.add(0,new BoChannelInfo());
		printTable(list);
		return "";
	}
	
}
